package com.daoImpl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.bean.ApplicablePart;
import com.bean.ProcureInquiry;
import com.bean.ProcureProduct;
import com.bean.ProductPart;
import com.bean.SupplierProduct;
import com.dao.ProcureInquiryDao;

/**
 * A data access object (DAO) providing persistence and search support for
 * ProcureInquiry entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.bean.ProcureInquiry
 * @author MyEclipse Persistence Tools
 */
public class ProcureInquiryDaoImpl extends HibernateDaoSupport implements ProcureInquiryDao {
	private static final Logger log = LoggerFactory
			.getLogger(ProcureInquiryDaoImpl.class);
	// property constants
	public static final String PROC_ID = "procId";
	public static final String SUID = "suid";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProcureInquiryDao#save(com.bean.ProcureInquiry)
	 */
	@Override
	public void save(ProcureInquiry transientInstance) {
		log.debug("saving ProcureInquiry instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProcureInquiryDao#delete(com.bean.ProcureInquiry)
	 */
	@Override
	public void delete(ProcureInquiry persistentInstance) {
		log.debug("deleting ProcureInquiry instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProcureInquiryDao#findById(java.lang.Integer)
	 */
	@Override
	public ProcureInquiry findById(java.lang.Integer id) {
		log.debug("getting ProcureInquiry instance with id: " + id);
		try {
			ProcureInquiry instance = (ProcureInquiry) getHibernateTemplate()
					.get("com.bean.ProcureInquiry", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProcureInquiryDao#findByExample(com.bean.ProcureInquiry)
	 */
	@Override
	public List findByExample(ProcureInquiry instance) {
		log.debug("finding ProcureInquiry instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProcureInquiryDao#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ProcureInquiry instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from ProcureInquiry as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProcureInquiryDao#findByProcId(java.lang.Object)
	 */
	@Override
	public List findByProcId(Object procId) {
		return findByProperty(PROC_ID, procId);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProcureInquiryDao#findBySuid(java.lang.Object)
	 */
	@Override
	public List findBySuid(Object suid) {
		return findByProperty(SUID, suid);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProcureInquiryDao#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all ProcureInquiry instances");
		try {
			String queryString = "from ProcureInquiry";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.ProcureInquiryDao#merge(com.bean.ProcureInquiry)
	 */
	@Override
	public ProcureInquiry merge(ProcureInquiry detachedInstance) {
		log.debug("merging ProcureInquiry instance");
		try {
			ProcureInquiry result = (ProcureInquiry) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ProcureInquiry instance) {
		log.debug("attaching dirty ProcureInquiry instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ProcureInquiry instance) {
		log.debug("attaching clean ProcureInquiry instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ProcureInquiryDao getFromApplicationContext(
			ApplicationContext ctx) {
		return (ProcureInquiryDao) ctx.getBean("procureInquiryDao");
	}

	@Override
	public List<ProcureInquiry> getSuinInquiryList(String table, String pk,
			String sort, int PageNumber, int PageSize, String querys) {
		Session session = getSession();  
		SQLQuery query=null;
		
		StringBuffer sqlbuf = new StringBuffer("execute [dbo].[sp_get_page_of_table2] ");
		sqlbuf.append("'"+table+"','"+pk+"','"+sort+"',"+PageNumber+","+PageSize+",'*',\""+querys+"\"");
		
		query=session.createSQLQuery(sqlbuf.toString());

		List<Object[]> resultSet = query.list();
		List<ProcureInquiry> list = new ArrayList<ProcureInquiry>();
		
		for (Object[] r : resultSet) {
			ProcureInquiry pi = new ProcureInquiry();
			ProcureProduct pp = new ProcureProduct();
			SupplierProduct sp = new SupplierProduct();
			
			pi.setPqId((Integer)r[0]);
			pi.setSendTime((Timestamp)r[1]);
			pi.setEndTime((Timestamp)r[2]);
			
			pp.setProcId((Integer)r[3]);
			pp.setProductCode((String)r[4]);
			pp.setStatus((Integer)r[5]);
			pp.setDeliveryDate((Timestamp)r[6]);
			pp.setBlueprint((String)r[7]);
			pp.setProcCount((Integer)r[8]);
			pp.setSysUser((String)r[9]);
			
			//sp.setSupId((Integer)r[10]);
			pi.setSuid((String)r[10]);
//			BigDecimal price=(BigDecimal)r[12];
//			if(price!=null){
//				sp.setPrice(price.doubleValue());
//			}
//			sp.setCreateTime((Timestamp)r[13]);
//			sp.setMemo((String)r[14]);
			
			pi.setProcureProduct(pp);
			sp.setProcureInquiry(pi);
			
			list.add(pi);
		}
		
		return list;
	}

}